<template>
  <van-field
    v-model="str"
    border
    clearable
    :placeholder="placeholder"
    @keyup.enter="onSearch"
    @focus="$emit('focus')"
    class="search-input"
  >
    <template #label v-if="showBack">
      <van-icon name="arrow-left" @click="$router.go(-1)" />
    </template>
    <template #button v-if="showBtn">
      <van-button round size="small" type="warning" @click="onSearch">
        搜索
      </van-button>
    </template>
  </van-field>
</template>
<script>
export default {
  props: {
    value: {
      type: String,
      default: ""
    },
    showBack: {
      type: Boolean,
      default: false
    },
    placeholder: {
      type: String,
      default: "搜索"
    },
    showBtn: {
      type: Boolean,
      default: true
    }
  },
  methods: {
    onSearch() {
      if (!this.str) {
        return this.$toast({
          type: "fail",
          icon: "fail",
          message: "搜索内容不能为空",
          duration: 1000
        });
      }
      this.$emit("onSearch", this.str);
    }
  },
  computed: {
    str: {
      get() {
        return this.value;
      },
      set(v) {
        this.$emit("input", v);
      }
    }
  }
};
</script>
<style lang="scss" scoped>
.search-input {
  background: #e9e9e9;
  padding: 0.2rem 0.32rem;
  font-size: 0.3rem;
  &::after {
    border: 0;
  }
  ::v-deep .van-field__control {
    padding: 0 1em;
    height: 0.6rem;
    border-radius: 0.3rem;
    border: 1px solid #fff;
  }

  ::v-deep .van-field__button {
    font-size: 0;
    .van-button {
      height: 0.6rem;
      padding: 0 0.2rem;
    }
  }
  ::v-deep .van-field__label {
    width: 20px;
    margin-right: 5px;
    display: flex;
    align-items: center;
    .van-icon {
      font-size: 18px;
    }
  }
}
</style>
